package etc.com.market;

import java.util.ArrayList;

import etc.com.sql.DBAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class InfomationDetailAct extends Activity implements
		OnItemClickListener {
	/** Called when the activity is first created. */
	private ListView lv;
	ArrayList<String> arrtext = new ArrayList<String>();
	ArrayList<String> arrTitle = new ArrayList<String>();
	private static final int REQUEST_CODE = 10;
	int[] idimage = new int[7];
	String str;
	String previous;
	String storename;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.informationdetail);

		lv = (ListView) findViewById(R.id.listview);
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(this);
		lv.setAdapter(new ImageAdapter(this));

		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		String messageReceive = extras.getString("message");

		// ImageView ImageView = (ImageView) findViewById(R.id.banner);
		// ImageView.setImageResource(R.drawable.header3);
		int position = extras.getInt("position");
		if (messageReceive != null) {
			// arrtext.add(messageReceive);
			str = messageReceive.toString();

			previous = str.substring(0, str.indexOf(";"));
			str = str.substring(str.indexOf(";") + 1);

			storename = str.substring(0, str.indexOf(";"));
			str = str.substring(str.indexOf(";") + 1);
			
			String infoId = str;
			DBAdapter dbAdapter = new DBAdapter(getApplicationContext());
			Cursor cursor = dbAdapter.getInfoTitle(infoId);
			cursor.moveToNext();
			String title = String.valueOf(position + 1) + "." +cursor.getString(1); 
			cursor.close();
			TextView view = (TextView) findViewById(R.id.btnNext);
			view.setText(title);

			TextView v = (TextView) findViewById(R.id.storename);
			v.setText(storename);
			
			Cursor cursorDetail = dbAdapter.getInfoDetail(infoId);
			while(cursorDetail.moveToNext()){
				arrtext.add(cursorDetail.getString(2));
				arrTitle.add(cursorDetail.getString(1));
			}
			cursorDetail.close();
//			TextView v1 = (TextView) findViewById(R.id.headerprevious);
//			v1.setText(previous);
		}
		
		
		
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		// Toast.makeText(this, "CHON:"+arg2, Toast.LENGTH_SHORT).show();
		// Intent activity2 = new Intent(this, activity2.class);
		// startActivity(activity2);

	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
	}
	// function use for footer button ==============================
	public void Previous(View view) {
		//onClickPre(view, storename);
		finish();
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void Home(View view) {
		Intent i = new Intent(this, StoreTopPage.class);
		i.putExtra("message", storename);
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void SearchProduct(View view) {
		onClickSearch(view, "店舗詳細;" + storename);
	}

	public void SpecifyProduct(View view) {
		onClickSpec(view, "店舗詳細;" + storename);
	}

	public void Info(View view) {
		onClickInfo(view, storename);
	}
	
	public void Contact(View view) {
		onClickContact(view, storename);
	}

	// ===================================================================

	public void onClickPre(View view, String message) {
		Intent i = new Intent(this, InfomationAct.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);

	}

	public void onClickSpec(View view, String message) {
		Intent i = new Intent(this, ListitemActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	 public void onClickSearch(View view, String message) {
			Intent i = new Intent(this, SearchOursProducts.class);
			i.putExtra("message", message);
			// Set the request code to any code you like, you can identify the
			// callback via this code
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);

		}
	
	public void onClickInfo(View view, String message) {
		Intent i = new Intent(this, InfomationAct.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void onClickContact(View view, String message) {
		Intent i = new Intent(this, SenmailActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public class ImageAdapter extends BaseAdapter {
		private Context mContext;

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return arrtext.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		// create a new ImageView for each item referenced by the Adapter
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(mContext);

			View row = inflater.inflate(R.layout.rowonlytext, parent, false);

			TextView label = (TextView) row.findViewById(R.id.namefile);
			TextView title = (TextView) row.findViewById(R.id.rowtitle);

			label.setText(arrtext.get(position));
			title.setText(arrTitle.get(position));

			return row;
		}
	}
}
